<?php

namespace App\Api\Helpers\OAuth;

use Laravel\Socialite\Two\AbstractProvider;
use Laravel\Socialite\Two\ProviderInterface;
use Laravel\Socialite\Two\User;

class WechatProvider extends AbstractProvider implements ProviderInterface
{

    protected $openId;

    protected function getTokenUrl()
    {
        return 'https://api.weixin.qq.com/sns/oauth2/access_token';
    }

    protected function mapUserToObject(array $user)
    {
        return (new User())->setRaw($user)->map([

            'openid'    => $user['openid'],
            'nickname'  => $user['nickname'],
            'avatar'    => $user['headimgurl'],
            'name'      => $user['nickname'],
            'email'     => null,
            'unionid'   => $user['unionid']
        ]);

    }

    protected function getUserByToken($token)
    {
        $response = $this->getHttpClient()->get('https://api.weixin.qq.com/sns/userinfo', [
            'query' => [
                'access_token' => $token,
                'openid'       => $this->openId,
                'lang'         => 'zh_CN',
            ],
        ]);

        return json_decode($response->getBody(), true);
    }

    protected function getAuthUrl($state)
    {
        return $this->buildAuthUrlFromBase('https://open.weixin.qq.com/connect/qrconnect', $state);
    }


}